﻿using System;
using System.Text;
using SpiderCore;
using SpiderCore.Spiders;
using SpiderCore.Utility;
using SpiderCore.Visited;

namespace ResourceCollectionSys.Spider
{
    /// <summary>
    /// 
    /// </summary>
    public class KanKanSprider : SpiderCore.Spiders.Spider
    {
        private IVisited _visited = new DBVisited();
        public override void StartToWork(ToDoData data)
        {
            try
            {
                string finnalurl = GetFinalUrl(data.PageUrl, data.Flag);
                PageData pageData = new PageData();
                switch (data.Flag)
                {
                    case "video":
                        pageData = GetPageResult(finnalurl);
                        break;

                }
                if (pageData != null)
                {
                    pageData.PageUrl = data.PageUrl;
                    PageRepository.AddPage(pageData);
                }
            }
            catch (Exception ex)
            {
                SpiderCaptain.GetInstance().StopToWork(ex.ToString());
                CustomsLog.WriteExceptionErrorLog("看看网爬虫工作异常", ex,data.PageUrl);
            }
        }

        protected override string GetFinalUrl(string sourceContent, string flag)
        {
            return sourceContent;
        }
        /// <summary>
        /// 处理页面
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private PageData GetPageResult(string url)
        {
            var html = GetSourceHtml(url, Encoding.Default);
            var links = GetLink(html, "kankan.com");
            foreach (var link in links)
            {
                //限定范围
                if (!link.StartsWith("http://vod.") && !link.StartsWith("http://movie.") && !link.StartsWith("http://pianhua.")
                    && !link.StartsWith("http://tv.") && !link.StartsWith("http://anime.") && !link.StartsWith("http://jilupian.")
                    && !link.StartsWith("http://open."))
                   continue;
                try
                {
                    _visited.AddUrl(link, null);
                    ToDoData d = new ToDoData();
                    d.Flag = "video";
                    d.Route = "KanKan";
                    d.PageUrl = link;
                    ToDo.AddUrl(d);

                }
                catch (Exception)
                {
                    //重复URL不管
                }
            }
            //详细页面才放入仓库
            if (url.EndsWith(".shtml"))
            {
                PageData page = new PageData()
                {
                    Data = html,
                    Flag = "video",
                    Route = "KanKan",
                    Date = DateTime.Now,
                    PageUrl = url
                };
                return page;
            }
            return null;
        }
    }
}
